﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DEFUSE;

namespace auxiliary_proj
{
    public partial class FormChangePass : Form
    {
        bool isNewPass = false;
        bool isRetypePass = false;

        //allows the user to start with 4 attempts to change password
        //everytime an invalid match is made on the current password, decrement
        //once it reaches 0, dispose of the window, and logout the user
        //static so the user doesn't close window and re-open to get 4 attempts again
        static int ATTEMPTS_REMAINING = 4;

        public FormChangePass()
        {
            InitializeComponent();

            //change form name to show user's First and Last name
            this.Text = "Update Password: " + Employee.FirstName + " " + Employee.LastName;

            //initially do not enable
            btnSubmit.Enabled = false;
            tbRetypePass.Enabled = false;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //new pass and re-type pass needs to be equal
            if (tbRetypePass.Enabled == false)
                MessageBox.Show("New password has invalid input!");
            else if (!isRetypePass)
                MessageBox.Show("The re-typed password doesn't match!");
            else
            {
                //new pass pattern is valid, and retype pass matches, test if current password is valid
                //tbCurrentPass is plain text pass, Employee.Password is hashed password
                if (!PasswordHash.ValidatePassword(tbCurrentPass.Text, Employee.Password))
                {
                    if (--ATTEMPTS_REMAINING > 0)
                        MessageBox.Show("The current password is invalid!\nYou have " + ATTEMPTS_REMAINING + " attempts remaining!");
                    else
                    {
                        /*0 attempts remaining initiate logout*/

                        //log out user
                        Employee.logout();
                    }
                }
                else
                {
                    //current password is validated
                    ATTEMPTS_REMAINING = 4;
                    //update user with new password (hashed)
                    Query.nonQuery("UPDATE USER SET password='" + PasswordHash.HashPassword(tbNewPass.Text) + "' WHERE user_name='" + Employee.User + "'");
                }
            }
            this.Dispose();
            //disable button until those are equal? since using keypress listeners
        }

        private void currentPassword_textChanged(object sender, EventArgs e)
        {
            //if tbCurrentPass is not empty, enable the button, else disable it
            if (((TextBox)sender).Text != "")
                btnSubmit.Enabled = true;
            else
                btnSubmit.Enabled = false;
        }

        private void newPassword_textChanged(object sender, EventArgs e)
        {
            //must start with letter, then 3-15 alpha-numeric and special characters (16 is max password length)
            isNewPass = Parser.isMatch(sender, @"^[a-zA-Z][\w!@#$%^&*\(\)-+';:<>`~]{3,15}$");

            if (isNewPass)
                tbRetypePass.Enabled = true;
            else
            {
                tbRetypePass.Enabled = false;
                tbRetypePass.BackColor = SystemColors.Info;
            }
        }

        private void retypePassword_textChanged(object sender, EventArgs e)
        {
            //pattern this time is the text from new password
            isRetypePass = Parser.isMatch(sender, tbNewPass.Text);
        }
    }
}
